package com.baosight.xCyber.input;

import com.baosight.ice.util.ConfLoader;
import com.baosight.xCyber.model.TagNode;
import com.baosight.xCyber.util.BuildCode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.*;

@Component
public class CodeLoad {

    private Map<String, TagNode> codeMap = new LinkedHashMap<>();
    private Map<String, TagNode> titleMap = new TreeMap<>();
    private Log logger = LogFactory.getLog(this.getClass());


    /**
     * 初始化加载code.properties
     */
    public void doInit(String codePath,Map<String,TagNode> dataMap) {

        String codeName = codePath.substring(codePath.lastIndexOf("/")+1);
        String treeName = codeName.substring(codeName.lastIndexOf("_")+1,
                codeName.lastIndexOf("."));
        if(dataMap.size() == 0){
            return;
        }
        File file = new File(codePath);
        if (!file.exists()||file.length()==0) {
            logger.info(treeName+":未检测到"+codeName+"文件或者树规则为空，采用系统自生成文件解析树");
            BuildCode.outPutCode(dataMap,codePath);
        }

        Properties properties = ConfLoader.getPropertie("code/"+codeName);
        if(properties==null){
            return;
        }
        //设置集合倒序储存
        titleMap=((TreeMap<String,TagNode>) titleMap).descendingMap();

        properties.keySet().forEach(key -> {
            TagNode tagNode = new TagNode();
            try {
                String tagName = (String) key;
                tagNode.tagName = tagName;
                tagNode.treeName = treeName;
                tagNode.showName = String.format(properties.getProperty(tagName).trim(),"utf-8");
                codeMap.put("root",new TagNode(treeName));
                if (tagName.toLowerCase().contains("title")) {
                    titleMap.put(tagName, tagNode);
                } else {
                    codeMap.put(tagName, tagNode);
                }
            } catch (Exception e) {
                logger.error(treeName+":文件字符编码格式不正确，读取失败:" + e.getMessage());
            }
        });
        logger.info(treeName+":"+codeName+"加载完毕");
    }

    public Map<String, TagNode> getDataMap(String treeName){
        Properties properties = ConfLoader.getPropertie("code/data.properties");
        Map<String,TagNode> datamap= new HashMap<>();
        properties.keySet().forEach(key -> {
            TagNode tagNode = new TagNode();
            String tagName = (String) key;
            tagNode.tagName = tagName;
            tagNode.treeName = treeName;
            tagNode.showName = properties.getProperty(tagName).trim();

            datamap.put(tagName, tagNode);
        });
        return datamap;
    }


    public Map<String, TagNode> getCodeMap() {
        return codeMap;
    }

    public void setCodes(Map<String, TagNode> codeMap) {
        this.codeMap = codeMap;
    }

    public Map<String, TagNode> getTitleMap() {
        return titleMap;
    }

    public void setTitleMap(Map<String, TagNode> titleMap) {
        this.titleMap = titleMap;
    }
}
